/*
*    Clean AJAX Engine v4.0
*    Copyright (C) 2005-2006 Carlos Eduardo Goncalves (cadu.goncalves@gmail.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program; if not, write to the Free Software
*    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
* Provides hash algorithms utils.
* @author Carlos Eduardo Goncalves
*/

/**
* <p>Generic hash tool constructor. </p>
*/
function HashTool(){
}

/**
* <p>Calculates the hash string for the value based on 
* algorithm defined on engine configuration (md5 or sha1).</p>
* @param value
*		<code>String</code> value to encode.
* @param return
*		Hash code calculated.
*/
HashTool.prototype.encodeString = function(value){
  if(env_HashAlgorithm.toLowerCase() == "md5") 
    return hex_md5(value);
  else
    return hex_sha1(value); 
}

/**
* <p>Calculates the hash string for the query string based on 
* algorithm defined on engine configuration (md5 or sha1).</p>
* @param query
*		Query string value to encode.
* @param return
*		Query string with values replaced by respective hash code.
*/
HashTool.prototype.encodeQuery = function(query){
  var result = "";
  var values = query.split("&");	
  for(var i = 0; i < values.length; ++i){
	 var value = values[i].split("=");
	 result += (value[0] + "=" + this.encodeString(value[1])) + "&";
  }	  
  return result.substr(0, (result.length - 1));
}